﻿//--------------------- Copyright Block ----------------------
/*


C# Code By: Tareq Ateik

License: GNU General Public License, ver 3

TERMS OF USE:
	Permission is granted to use this code, with or
	without modification, in any website or application
	provided that credit is given to the original work
	with a link back to ateik.me

This program is distributed in the hope that it will
be useful, but WITHOUT ANY WARRANTY.

PLEASE DO NOT REMOVE THIS COPYRIGHT BLOCK.

*/

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using System.IO.IsolatedStorage;
using System.Device.Location;
using System.Xml.Linq;

namespace PrayerTime
{
    public partial class Settings : PhoneApplicationPage
    {
        #region
        IsolatedStorageSettings iso = IsolatedStorageSettings.ApplicationSettings;
        WebClient GeocodingWebClient = new WebClient();
        PrayerSettings PrayerSettings = new PrayerSettings();
        List<ResearchResult> ResearchResults;
        #endregion
        public Settings()
        {
            InitializeComponent();
            GeocodingWebClient.DownloadStringCompleted += new DownloadStringCompletedEventHandler(GeocodingWebClient_DownloadStringCompleted);
        }
        protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
        {
            if (!iso.TryGetValue("PrayerSettings", out PrayerSettings))
            {
                PrayerSettings = new PrayerSettings { CalculationMethod = CalcuationMethod.Makkah, CurrentCity = "", CurrentLocation = new GeoCoordinate(0, 0), UseGPS = false, AsrMethod= AsrMethod.Shafii };

            }
            else
            {
                lstCalculationMethod.SelectedIndex = (int)PrayerSettings.CalculationMethod;
                lstAsrMethod.SelectedIndex = (int)PrayerSettings.AsrMethod;
            }
            base.OnNavigatedTo(e);
        }
        void GeocodingWebClient_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
        {
            XDocument document = XDocument.Parse(e.Result);

            var data = (from query in document.Descendants("result")
                        select new ResearchResult()
                        {
                            Location = new GeoCoordinate((double)query.Element("geometry").Element("location").Element("lat"), (double)query.Element("geometry").Element("location").Element("lng")),
                            City = (string)query.Element("formatted_address")
                        }).Distinct();
            List<string> ListBoxValues = new List<string>();
            ResearchResults = data.ToList<ResearchResult>();
            foreach (ResearchResult r in data)
                ListBoxValues.Add(r.City);
            listBox1.ItemsSource = ListBoxValues;

            progressBar.Visibility = System.Windows.Visibility.Collapsed;
            progressBar.IsIndeterminate = false;
        }
        private void button1_Click(object sender, RoutedEventArgs e)
        {
            if (!String.IsNullOrEmpty(textBox1.Text))
            {
                progressBar.Visibility = System.Windows.Visibility.Visible;
                progressBar.IsIndeterminate = true;
                GeocodingWebClient.DownloadStringAsync(new Uri("http://maps.googleapis.com/maps/api/geocode/xml?address=" + textBox1.Text + "&sensor=false&language=en"));
            }
        }
        private void ApplicationBarIconButton_Click_1(object sender, EventArgs e)
        {
            PrayerSettings.CurrentCity = (listBox1.SelectedItem == null) ? PrayerSettings.CurrentCity : listBox1.SelectedItem.ToString();
            PrayerSettings.CurrentLocation = (listBox1.SelectedItem == null) ? PrayerSettings.CurrentLocation : ResearchResults[listBox1.SelectedIndex].Location;
            PrayerSettings.CalculationMethod = (CalcuationMethod)lstCalculationMethod.SelectedIndex;
            PrayerSettings.AsrMethod = (AsrMethod)lstAsrMethod.SelectedIndex;
            PrayerSettings.UseGPS = false;
            iso["PrayerSettings"] = PrayerSettings;
            NavigationService.GoBack();
        }       
        private void textBox1_KeyUp(object sender, KeyEventArgs e)
        {
            if(e.Key == Key.Enter)
                if (!String.IsNullOrEmpty(textBox1.Text))
                {
                    progressBar.Visibility = System.Windows.Visibility.Visible;
                    progressBar.IsIndeterminate = true;
                    GeocodingWebClient.DownloadStringAsync(new Uri("http://maps.googleapis.com/maps/api/geocode/xml?address=" + textBox1.Text + "&sensor=false&language=en"));
                    button1.Focus();
                }
        }
    }
}